<?php

namespace App\Http\Controllers\Game\Config;

use App\Services\Game\Setting\Config\CommonService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Game\Config\Betting\SaveRequest;
use App\Http\Resources\Common\DatasResource;
use App\Http\Resources\Common\DataResource;
use App\Services\Game\Config\BettingService;

class BettingController extends Controller
{

    /**
     * 获取数据列表
     */
    public function getList(Request $request)
    {
        list ($collection, $total) = BettingService::make()->getList($request);

        return DatasResource::make($collection)->setPaginateTotal($total);
    }

    /**
     * bet1覆盖bet
     * @return DataResource
     */
    public function resettingBetData()
    {
        BettingService::make()->resettingBetData();

        return new DataResource([]);
    }


    /**
     * 获取单条游戏数据
     * @pararm string $game_id
     */
    public function getGameDetails(Request $request)
    {
        $game_id = $request->input('game_id');

        $config = CommonService::make()->getConfig($game_id);

        return new DataResource([
            'bet_status' => $config['globals']['bet_status'] ?? 0
        ]);
    }


    /**
     * 保存数据
     *
     * @param int $id
     */
    public function save(SaveRequest $request)
    {
        BettingService::make()->save($request);

        return new DataResource(null);
    }

    /**
     * 删除数据
     *
     * @param int $id
     */
    public function destroy(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->input('id');

        BettingService::make()->destroy($id);

        return new DataResource([$id]);
    }

    /**
     * 使用来源游戏覆盖目标游戏投注配置
     */
    public function fullGameBets(Request $request)
    {
        $data = $request->validate([
            'sourceGameId' => 'required|string|max:64',
            'targetGameIds' => 'required|array|min:1',
        ]);

        BettingService::make()->fullGameBets(
            $data['sourceGameId'],
            $data['targetGameIds']
        );

        return new DataResource($data);
    }
}
